var app = getApp()
var page;
var listsize;
Page({
    data: {
        /** 
            * 页面配置 
            */
        winWidth: 0,
        winHeight: 0,
        // tab切换  
        currentTab: 0,
        status:'',
        imgurl:app.globalData.imgurl,
    },
    onLoad: function () {
        var that = this;
        this.getUserInfo()
        /** 
         * 获取系统信息 
         */
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    winWidth: res.windowWidth,
                    winHeight: res.windowHeight
                });
            }

        });
    },
    
    onShow:function(){
      page = 1
      this.getlist(this.data.status)
      this.getJifen() //积分
    },
    getJifen:function(){
      var that = this;
      wx.request({
        url: app.globalData.url + 'findByUserId?userId=' + wx.getStorageSync('userId'),
        header: {},
        success: function (res) {
          var personalinfo = res.data.body;
          that.setData({
            personalinfo: personalinfo
          })
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    },
    // 获取用户信息
    getUserInfo:function(){
        var userInfo = wx.getStorageSync('userInfo');
        this.setData({
          userInfo:userInfo
        })
    },
    // 获取订单列表
    getlist:function(status){
      var that = this;
      var userId = wx.getStorageSync('userId');
      wx.showLoading({
        title: '正在加载',
      })
      if(page == 1){
       var list = []
      }else{
       var list = this.data.signupList
      }
      wx.request({
        url: app.globalData.url +'signupList',
        data:{
          userId: userId,
          pageNo:page,
          status: status == '-1' && status == undefined ? '' : status
        },
        success:function(res){
          if(res.data.success){
            var _list = res.data.body.signupList;
            if (_list == '') { _list = []}
            _list.forEach(function(item){
              switch(item.status){
                case 0:
                item.statustr = '已报名';
                break;
                case 1:
                  item.statustr = '已录用';
                  break;
                case 2:
                  item.statustr = '已到岗';
                  break;
                case 3:
                  item.statustr = '已结束';
                  break;
                case 4:
                  item.statustr = '录用失败';
                  break;
                case 5:
                  item.statustr = '已到岗';
                  break;
              }
            })
              
          }else{
           var  _list = []
          }
          listsize = _list.length;
          if(listsize == 0){
            var more = '没有更多啦'
          }else{
            var more = '加载更多'
          }
          page++;
          var signupList = list.concat(_list)
          console.log(signupList)
          that.setData({
            signupList: signupList,
            more: more
          })
        },
        fail:function(){
        },
        complete:function(){
          setTimeout(function(){
            wx.hideLoading()
          },500)
        }
      })
    },
    /** 
       * 滑动切换tab 
       */
    bindChange: function (e) {
        var that = this;
        that.setData({ currentTab: e.detail.current });

    },
    /** 
     * 点击tab切换 
     */
    swichNav: function (e) {
        var that = this;
        var o = e.currentTarget.dataset;
        var status = o.current - 1;
        if (status == -1) {
          status = ''
        }
        console.log(o)
        if (this.data.currentTab === e.target.dataset.current) {
            return false;
        } else {
          // 获取订单列表
          page = 1
         
          that.getlist(status)
            that.setData({
              status: status,
              currentTab: o.current
            })
        }
    },
    // 电话拨打 
    call:function(e){
      var o = e.currentTarget.dataset;
      wx.makePhoneCall({
        phoneNumber: o.mobile, 
      })
    },
    // 取消订单
    cancel:function(e){
      var that = this;
      var o = e.currentTarget.dataset;
      wx.showModal({
        title: '提示',
        content: '您确定要取消报名吗?',
        showCancel: true,
        success: function(res) {
          if(res.confirm == true){
            wx.request({
              url: app.globalData.url + 'cancelSignup?signupId=' + o.id,
              success: function (res) { 
                if(res.data.success){
                  wx.showToast({
                    title: '取消报名成功',
                    icon: 'success',
                    duration: 1500,
                    mask: true,
                    success: function (res) {
                      page = 1;
                      that.getlist(that.data.status)
                    },
                  })
                }
               
               },
              fail: function (res) { },
              complete: function (res) { },

            })
          }
        },
        fail: function(res) {},
        complete: function(res) {},
      })
    
    },
    onReachBottom:function(){
      if (listsize > 0) {
        this.getlist(this.data.status)

      }
    }
}) 